let add_patient=new Vue({
    el:"#add",
    data:{
        departments:[],
        doctors:[],
        officeId:"ABC",
        doctorId:'',
        name:"",
        idCard:"",
        phone:"",
        headImg:"",
        history:"",
        gender:"",
        file:""
    },
    methods:{
        //异步请求加载科室名称
        loadDepartments:function() {
            axios({
                url : "/add_appointment/department",
                method:"get",
            }).then(function(r) {
                add_patient.departments=r.data;
                console.log(add_patient.departments)
            })
        },
        //异步请求加载选中科室的医生
        loadDoctors:function() {
            console.log("接收到了要加载的科室id:"+add_patient.officeId);
            //判断选中的不是"请选择---"那个选项
            if(add_patient.officeId==="ABC"){
                add_patient.doctors=[];
                return;
            }
            //发出异步请求,上传科室的id
            axios({
                url:"/portal/doctors/getdoctorsbyofficeid",
                method:'get',
                params:{
                    id:add_patient.officeId
                }
            }).then(function(r) {
                add_patient.doctors=r.data;
            })

        },
        //获取并显示上传的图片
        getFile:function (event) {
            //获取发生事件input元素的第一个文件
            add_patient.file=event.target.files[0];
            //创建一个fileReader对象
            var fileReader=new FileReader();
            //读取图片文件数据为DataUrl
            fileReader.readAsDataURL(add_patient.file)
            //读取文件
            fileReader.onload=function(e) {
                //将这个url绑定到img元素的src属性
                $("#img").attr("src",e.target.result)
            }
        },
        //上传添加的病人信息
        submitpatient:function() {
            // console.log("输入的办公室id:"+add_patient.officeId);
            // console.log("输入的医生id:"+add_patient.doctorId);
            // console.log("输入的姓名:"+add_patient.name);
            // console.log("输入的身份证号:"+add_patient.idCard);
            // console.log("输入的电话:"+add_patient.phone);
            // console.log("输入的头像:"+add_patient.headImg);
            // console.log("输入的历史:"+add_patient.history);
            //获取性别选项的结果
            add_patient.gender=$(":radio:checked").val();
            //生成form表单
            let form=new FormData;
            form.append("name",add_patient.name)
            form.append("idCard",add_patient.idCard)
            form.append("doctorId",add_patient.doctorId)
            form.append("departmentId",add_patient.officeId)
            form.append("idCard",add_patient.idCard)
            form.append("phone",add_patient.phone)
            form.append("headImg",add_patient.file)
            form.append("history",add_patient.history)
            form.append("gender",add_patient.gender)
            //异步发送post请求上传信息
            axios({
                url:"/portal/patient/addpatient",
                method:"post",
                data:form
            }).then(function (r) {
                console.log(r.data )
            })

        },
        //清空所有信息
        clearAll:function(){
                add_patient.doctorId=""
                add_patient. name=""
                add_patient. idCard=""
                add_patient. phone=""
                add_patient. headImg=""
                add_patient.  history=""
            add_patient. file=""
            $("#img").attr("src","../../assets/img/dashboard/headmodel.jpg ")
            add_patient.doctors=[]
            add_patient.officeId="ABC"
        }

    },
    created:function () {
        //页面加载后运行以下方法
        this.loadDepartments();
    }

})